package com.jzr.medical.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.medical.common.LayServiceImpl;
import com.jzr.medical.db1.mapper.TRoleMapper;
import com.jzr.medical.db1.mapper.TUserMapper;
import com.jzr.medical.db1.mapper.TUserRoleRefMapper;
import com.jzr.medical.db1.model.TUser;
import com.jzr.medical.db1.service.UserService;
import com.jzr.medical.db1.vo.DeptCodeVo;
import com.jzr.medical.db1.vo.UserInfoVo;
import com.jzr.medical.db1.vo.UserMenuVo;
import com.jzr.medical.db1.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends LayServiceImpl<TUserMapper,TUser,UserVo,Integer> implements UserService {
    @Autowired
    private TUserRoleRefMapper userRoleRefMapper;

    @Autowired
    private TRoleMapper roleMapper;

    @Override
    public TUser findByUserCode(String userCode){
        QueryWrapper<TUser> qw = new QueryWrapper<>();
        qw.eq(TUser.UserCode_,userCode);
        List<TUser> list = baseMapper.selectList(qw);
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public  List<String> getRoleCodesByUserCode(String userCode){
        return baseMapper.getRoleCodesByUserCode(userCode);
    }
    @Override
    public  List<String> getRoleCodesById(Integer id){
        return baseMapper.getRoleCodesById(id);
    }

    @Override
    //@Cacheable(cacheNames = {"user.getUserDetailByUserName"},key = "#userName", unless = "#result == null")
    public UserDetails getUserDetailByUserCode(String userCode){
        TUser tUser = this.findByUserCode(userCode);

        if(tUser == null){
            //throw exception inform front end not this user
            throw new UsernameNotFoundException("user + " + userCode + "not found.");
        }

        List<String> roleList = this.getRoleCodesByUserCode(userCode);
        List<GrantedAuthority> authorities = roleList.stream()
                .map(role -> new SimpleGrantedAuthority(role)).collect(Collectors.toList());

        return new org.springframework.security.core.userdetails
                .User(userCode,tUser.getPassword(),authorities);
    }


    @Override
    public Integer getIdByUserCode(String userCode) {
        return baseMapper.getIdByUserCode(userCode);
    }

    @Override
    public String getPswById(Integer userId){
        return baseMapper.getPasswordById(userId);
    }

    @Override
    public Integer changePsw(Integer userId, String oldPsw, String newPsw){
        return baseMapper.changePsw(userId,oldPsw,newPsw);
    }

    @Override
    public List<UserMenuVo> getMenuByUserId(Integer userId) {
        return baseMapper.getMenuByUserId(userId);
    }

    @Override
    public UserInfoVo getUserInfoVoById(Integer id) {
        return baseMapper.getUserInfoVoById(id);
    }

    @Override
    public List<DeptCodeVo> getDeptList(Integer userId) {
        return baseMapper.getDeptList(userId);
    }
}
